package cn.tedu.qxhouserent.passport.controller;

import cn.tedu.qxhouserent.commons.pojo.vo.PageData;
import cn.tedu.qxhouserent.commons.web.JsonResult;
import cn.tedu.qxhouserent.passport.pojo.param.RoleAddNewParam;
import cn.tedu.qxhouserent.passport.pojo.param.RoleUpdateInfoParam;
import cn.tedu.qxhouserent.passport.pojo.vo.RoleListItemVO;
import cn.tedu.qxhouserent.passport.pojo.vo.RoleStandardVO;
import cn.tedu.qxhouserent.passport.service.RoleService;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/role")
@Api(tags = "02. 角色管理模块")
@Slf4j
@Validated
public class RoleController {

    @Autowired
    RoleService roleService;

    @PostMapping("add-new")
    @ApiOperation("添加角色")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/ams/admin/add-new')")
    public JsonResult addNew(@Valid RoleAddNewParam roleAddNewParam) {
        log.debug("开始处理【添加角色】的请求，参数：{}", roleAddNewParam);
        roleService.addNew(roleAddNewParam);
        log.debug("处理【添加角色】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除角色")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "角色ID",required = true,dataType = "long"),

    })
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    public JsonResult delete(@Range(min = 1,message = "根据ID删除角色失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据id删除角色】的请求，参数：{}", id);
        roleService.deleteById(id);
        log.debug("处理【根据id删除角色】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("update")
    @ApiOperation("修改角色")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult updateInfoById(@Range(min = 1,message = "根据ID更新角色失败，请提交合法的ID值！")
                                     @RequestParam Long id,
                                     @Valid RoleUpdateInfoParam roleUpdateInfoParam) {
        log.debug("开始处理【修改角色】的请求，参数：{}", id, roleUpdateInfoParam);
        roleService.updateInfoById(id, roleUpdateInfoParam);
        log.debug("处理【修改角色】的请求，完成！");
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("查询角色列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "query", value = "查询类型", paramType = "query", dataType = "string")
    })
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult list(@Range(min = 1, message = "查询角色列表失败，请提供正确的页码值！") Integer page,String query) {
        log.debug("开始处理【查询角色列表】的请求，页码：{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<RoleListItemVO> pageData = null;
        if ("all".equals(query)) {
            pageData = roleService.list(page, Integer.MAX_VALUE);
        } else {
            pageData = roleService.list(page);
        }
        return JsonResult.ok(pageData);
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询角色详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询角色详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询角色详情】的请求，参数：{}", id);
        RoleStandardVO result = roleService.getStandardById(id);
        return JsonResult.ok(result);
    }
}
